package br.com.emmis.tercons;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import br.com.emmis.tercons.receiver.AlarmReceiver;
import br.com.emmis.tercons.receiver.BootReceiver;
import br.com.emmis.tercons.services.CheckIntentionsService;

/**
 * Created by vaner on 10/12/2014.
 */
public class TercoApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        CheckIntentionsService.startActionDownloadCreateDonationXML(getApplicationContext());

        Intent downloader = new Intent(getApplicationContext(), AlarmReceiver.class);
        PendingIntent recurringDownload = PendingIntent.getBroadcast(getApplicationContext(),
                0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarms = (AlarmManager) getApplicationContext().getSystemService(
                Context.ALARM_SERVICE);

        alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                AlarmManager.INTERVAL_HALF_HOUR,
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, recurringDownload);

        ComponentName receiver = new ComponentName(getApplicationContext(), BootReceiver.class);
        PackageManager pm = getApplicationContext().getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }
}
